<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/27
 * Time: 上午12:24
 */

namespace Home\Model;
use Think\Model;

class ResumeModel extends Model{
    protected $tableName = 'resume';
    protected $pk        = 'resume_id';

    public function getFetcherYFInfo($user_id){
        $resume_field = array('resume_id','user_id','introduction','is_public','salary','ispublic_one');
        $resume_map['user_id'] = array('eq',$user_id);
        $resume_result = $this->field($resume_field)->where($resume_map)->select();
        //dump($resume_result);
        $ispublic_one_arr = explode(",",$resume_result[0]['ispublic_one']);
        //dump($ispublic_one_arr);
        $publicToOne = 0;
        for($i=0;$i < count($ispublic_one_arr);$i++){
            if( $ispublic_one_arr[$i] == $user_id ){
                $publicToOne = 1;
            }
        }
        if(1 == $resume_result[0]['is_public'] || 1== $publicToOne ){
            //全部公开乙方信息
            $resume_result[0]['public_resume'] = 2;
            //获取user_ext表中工作信息
            $user_ext_field = array('jobs');
            $user_ext_map['user_id'] = array('eq',$resume_result[0]['user_id']);
            $user_ext_db = M('User_ext');
            $user_ext_result = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
            //dump($user_ext_result);
            if(is_array($user_ext_result)){
                $resume_result[0]['jobs'] = $user_ext_result[0]['jobs'];
            }else{
                //获取user_ext失败
            }

            //获取简历加项resume_att
            $resume_att_db = M('Resume_att');
            $resume_att_map['resume_id'] = array('eq',$resume_result[0]['resume_id']);
            $resume_att_result = $resume_att_db->where($resume_att_map)->select();
            //dump($resume_att_result);
            if(is_array($resume_att_result)){
                $resume_att_fix = array();
                $signal = 0;
                for($i =1 ; $i < 9 ; $i ++){
                    if( $resume_att_result[0]['image_url'.$i] != NULL ){
                        $resume_att_fix['image_url'][$signal] = array( 'image_url'.$i => $resume_att_result[0]['image_url'.$i]);
                        $signal += 1 ;
                    }
                }
                if( NULL != $resume_att_result[0]['video_url1']){
                    $resume_att_fix['video_url'] = array('video_url1'=>$resume_att_result[0]['video_url1']);
                }
                $resume_result[0]['resume_att'] = $resume_att_fix;
            }else{
                //获取简历加项失败 resume_att
            }

            //获取experience表中工作经历信息
            $experience_field = array('exp_id','project_name','role','url','r_order');
            $experience_order = '`r_order` asc,`update_time` desc';
            $experience_map['resume_id'] = array('eq',$resume_result[0]['resume_id']);
            $experience_db = M('Experience');
            $experience_result = $experience_db->field($experience_field)->order($experience_order)->where($experience_map)->select();
            //dump($experience_result);
            if(is_array($experience_result)){
                $resume_result[0]['experience'] = $experience_result;
                foreach($experience_result as &$experience_result_one){
                    $attachment_field = array('image_url');
                    $attachment_map['exp_id'] = array('eq',$experience_result_one['exp_id']);
                    $attachment_db = M('Attachment');
                    $attachment_result = $attachment_db->field($attachment_field)->where($attachment_map)->select();
                    //dump($attachment_result);
                    if(is_array($attachment_result)){
                        $experience_result_one['attachment'] = $attachment_result;
                    }else{
                        //获取attachement失败
                    }
                }

                $resume_result[0]['experience'] = $experience_result;
                //dump($experience_result);
            }else{
                //获取experience失败
            }
        }else{
            //部分公开乙方信息
            $resume_result[0]['public_resume'] = 1;
            //获取user_ext表中工作及学历信息
            $user_ext_field = array('jobs','degree_year','degree_school','degree_major','ispublic_degree','level_j','level_y');
            $user_ext_map['user_id'] = array('eq',$resume_result[0]['user_id']);
            $user_ext_db = M('User_ext');
            $user_ext_result = $user_ext_db->field($user_ext_field)->where($user_ext_map)->select();
            if(is_array($user_ext_result)){
                $resume_result[0]['jobs'] = $user_ext_result[0]['jobs'];
                $resume_result[0]['level_j'] = $user_ext_result[0]['level_j'];
                $resume_result[0]['level_y'] = $user_ext_result[0]['level_y'];
                if(1 == $user_ext_result[0]['ispublic_degree']){
                    $resume_result[0]['degree_year'] = $user_ext_result[0]['degree_year'];
                    $resume_result[0]['degree_school'] = $user_ext_result[0]['degree_school'];
                    $resume_result[0]['degree_major'] = $user_ext_result[0]['degree_major'];
                }else{
                    $resume_result[0]['degree_year'] = '';
                    $resume_result[0]['degree_school'] = '';
                    $resume_result[0]['degree_major'] = '';
                }
            }else{
              //获取user_ext失败
            }

            //获取user表中名字头像
            $user_field = array('user_name','image_url','status');
            $user_map['user_id'] = array('eq',$resume_result[0]['user_id']);
            $user_db = M('User');
            $user_result = $user_db->field($user_field)->where($user_map)->select();
            if(is_array($user_result)){
                $resume_result[0]['user_name'] = $user_result[0]['user_name'];
                $resume_result[0]['image_url'] = $user_result[0]['image_url'];
                $resume_result[0]['status'] = $user_result[0]['status'];
            }else{
                //获取user表失败
            }

            //获取experience表中工作经历信息
            $experience_field = array('project_name','role');
            $experience_order = '`update_time` desc';
            $experience_map['resume_id'] = array('eq',$resume_result[0]['resume_id']);
            $experience_db = M('Experience');
            $experience_result = $experience_db->field($experience_field)->order($experience_order)->where($experience_map)->select();
            if(is_array($experience_result)){
                $resume_result[0]['experience'] = $experience_result[0];
            }else{
                //获取experience失败
            }

            //获取collect表收藏信息
            $collect_map['collect_type'] = 1;
            $collect_map['collecter_id'] = array('eq',$_SESSION['loginUser']['user_id']);
            $collect_map['collected_id'] = array('eq',$user_id);
            $collect_db = M('Collect');
            $collect_result = $collect_db->where($collect_map)->select();
            if(is_array($collect_result)){
                $resume_result[0]['collect_status'] = 2;
            }else{
                $resume_result[0]['collect_status'] = 1;
            }

        }
        return $resume_result[0];

    }

    //创建简历
    public function setResume($user_id){
        $data['user_id'] = $user_id;
        ini_set('date.timezone','Asia/Shanghai');
        $data['update_time'] = date('Y-m-d H:i:s',time());
        $data['is_public'] = 1;
        $resume_result = $this->add($data);
        if($resume_result){
            return true;
        }else{
            return false;
        }

    }

    //对他公开简历
    public function setPublicToOne($ispublic_one,$user_id){
        $resume_result1 = $this->where(array('user_id'=>$user_id))->select();
        if( NULL == $resume_result1[0]['ispublic_one']){
            $data['ispublic_one'] = $ispublic_one;
        }else{
            $signal = 0;
            $ispublic_one_arr = explode(",",$resume_result1[0]['ispublic_one']);
            for( $i = 0;$i < count($ispublic_one_arr);$i++ ){
                if( $ispublic_one == $ispublic_one_arr[$i] ){
                    $signal = 1;
                }
            }
            if( 1 == $signal ){
                return true;
            }else{
                $data['ispublic_one'] = $resume_result1[0]['ispublic_one'].",".$ispublic_one;
            }
        }
        $resume_result2 = $this->where(array('user_id'=>$user_id))->save($data);
        if($resume_result2){
            return true;
        }else{
            return false;
        }
    }

    public function deleteResumeImg($resume_id,$image_name){
        $resume_att_db = M('Resume_att');
        //$resume_att_map[$image_name] = array('eq',$image_url);
        $resume_att_map['resume_id'] = array('eq',$resume_id);
        $data[$image_name] = '';
        $resume_att_result = $resume_att_db->where($resume_att_map)->save($data);
        if($resume_att_result){
            return true;
        }else{
            return false;
        }
    }

    public function deleteResumeVideo($resume_id){
        $resume_att_db = M('Resume_att');
        $resume_att_map['resume_id'] = array('eq',$resume_id);
        $data['video_url1'] = '';
        $resume_att_result = $resume_att_db->where($resume_att_map)->save($data);
        if($resume_att_result){
            return true;
        }else{
            return false;
        }
    }

    public function setResumeImg($user_id,$image_name,$image_url){
        $resume_field = array('resume_id');
        $resume_map['user_id'] = array('eq',$user_id);
        $resume_result = $this->field($resume_field)->where($resume_map)->select();
        if(is_array($resume_result)){
            foreach($resume_result as &$resume_result_one){
                $resume_att_db = M('Resume_att');
                $data[$image_name] = $image_url;
                $resume_att_map['resume_id'] = array('eq',$resume_result_one['resume_id']);
                //判断是否存在加项
                $resume_att_exist = $resume_att_db->where($resume_att_map)->select();
                if(is_array($resume_att_exist)){
                    $resume_att_result = $resume_att_db->where($resume_att_map)->save($data);
                }else{
                    $data['resume_id'] = $resume_result_one['resume_id'];
                    $resume_att_result = $resume_att_db->add($data);
                }
                if($resume_att_result){
                    return true;
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
    }

} 